package com.freshcity.utils
{
	import com.freshcity.core.GlobalVar;
	
	import flash.events.TextEvent;
	import flash.text.StyleSheet;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	import flash.utils.ByteArray;

	public class StringUtil
	{
		public static var style:StyleSheet = new StyleSheet();
		
		{
			style.setStyle("a:link", { color: '#0000ff',fontSize:'12' });
			style.setStyle("a:hover",{color:'#ff0000',textDecoration: 'underline',fontSize:'12'});
			style.setStyle("a:visited",{color:'#990000',fontSize:'12'});
			style.setStyle("a:active ",{color:'#000000',fontSize:'12'});
		}
		
		private static var delHtmlreg:RegExp;
		{
			delHtmlreg=new RegExp("</?[^<>]+>", "gmi");
		}
		
		public static function addColor(content:String, color:String):String
		{
			return "<font color=\'" + color + "\'>" + content + "</font>";
		}
		
		public static function addBold(content:String, color:String):String
		{
			return "<b color=\'" + color + "\'>" + content + "</b>";
		}
		
		public static function getHtmlStyleText(content:String, color:String="#DDDDDD",fontsize:uint=12,fontFace:String="宋体"):String
		{
			return "<font color=\'"+color+"\' face=\'"+fontFace+"\' size=\'"+fontsize+"\' >" + content + "</font>";
		}
		
		public static function whoDesc(name:String):String
		{
			if(name==GlobalVar.LOGIN_USER.id){
				return "我";
			}else if(GlobalVar.LOGIN_USER.sex=='female'){
				return "他";
			}else{
				return "她";
			}
		}
		
		public static function addHyLink(label:String,value:String,linkHandler:Function):TextField
		{
			var list:TextField = new TextField();
			list.autoSize = TextFieldAutoSize.LEFT;
			list.multiline = true;
			list.styleSheet = style;
			
			list.htmlText = "".concat("<a href='event:",value,"'>",label,"</a>");
			list.addEventListener(TextEvent.LINK, linkHandler,false,0,true);
			return list;
		}
		
		public static function getTextField(label:String):TextField
		{
			var tf:TextField = new TextField();
			tf.autoSize = TextFieldAutoSize.LEFT;
			
			var format:TextFormat = new TextFormat("宋体",12);
			tf.defaultTextFormat = format;
			tf.htmlText = label;
			return tf;
		}		
		
		public static function formatDate(date:Date):String
		{
			var mon:int = date.month;
			var d:int = date.date;
			
			return "".concat(mon,"月",d,"日");	
		}		
		
		public static function dateToStr(date:Date):String
		{
			var year:int = date.fullYear;
			var mon:int = date.month;
			var d:int = date.date;
			
			var hour:int = date.hours;
			var min:int = date.minutes;
			var sec:int = date.seconds;
			
			var hourStr:String=hour < 10 ? ("0" + String(hour)) : String(hour);	
			var minStr:String=min < 10 ? ("0" + String(min)) : String(min);
			//var secStr:String=sec < 10 ? ("0" + String(sec)) : String(sec);
			
			return "".concat(mon,"-",d," ",hourStr,":",minStr);	
		}
		
		public static function strToDate(dateStr:String):Date
		{
			var parts:Array      = dateStr.toUpperCase().split(' ');
			var date:Array       = parts[0].split('-');
			var time:Array       = (parts.length <= 1) ? new Array() : parts[1].split(':');
			
			var year:uint        = GlobalUtil.isEmpty(date[0]) ? 0 : Number(date[0]);
			var month:uint       = GlobalUtil.isEmpty(date[1]) ? 0 : Number(date[1] - 1);
			var day:uint         = GlobalUtil.isEmpty(date[2]) ? 1 : Number(date[2]);
			var hour:int         = GlobalUtil.isEmpty(time[0]) ? 0 : Number(time[0]);
			var minute:uint      = GlobalUtil.isEmpty(time[1]) ? 0 : Number(time[1]);
			var second:uint      = 0;
			var millisecond:uint = 0;
			
			return new Date(year, month, day, hour, minute, second, millisecond);
		}		
		
		public static function getNow():String
		{
			var date:Date = new Date();
			
			var hour:int = date.hours;
			var min:int = date.minutes;
			var sec:int = date.seconds;
			
			var hourStr:String=hour < 10 ? ("0" + String(hour)) : String(hour);	
			var minStr:String=min < 10 ? ("0" + String(min)) : String(min);
			//var secStr:String=sec < 10 ? ("0" + String(sec)) : String(sec);
			
			return "".concat(hourStr,":",minStr);
		}
		
		
		//将一个整数的时间值格式化为00:00:00格式的时间值
		public static function formatTime(time:uint):String
		{
			var str:String;
			var hour:uint = Math.floor(time / 3600);
			var minutes:uint = Math.floor((time%3600)/60);
			var second:uint = time%60;
			var hs:String = "";
			if(hour>0){
				hs += hour >=10 ? ""+hour : "0"+hour;
				hs += ":";
			}
			var ms:String = minutes >=10 ? ""+minutes : "0"+minutes;
			var ss:String = second >=10 ? ""+second : "0"+second;
			str = hs+ms+":"+ss;
			return str;
		}
		
		public static function getTime():String
		{
			const d:String = '.';
			const e:String = '';
			const s:String = ':';
			
			var date:Date = new Date();
			return e.concat(pad(date.hours, 2), s, 
				pad(date.minutes, 2), s, 
				pad(date.seconds, 2), d, 
				pad(date.milliseconds, 3));
			
			function pad(value:String, length:int):String
			{
				const zero:String = '0';
				var result:String = value;
				while (result.length < length)
				{
					result = zero.concat(result);
				}
				return result;
			}
		}		
		
		public static function removeHtml(arg1:String):String
		{
			if (arg1)
			{
				return arg1.replace(delHtmlreg, "");
			}
			return "";
		}
		
		public static function isHtml(arg1:String):Boolean
		{
			if (arg1)
			{
				return arg1.search(delHtmlreg) > -1;
			}
			return false;
		}
		
		public static function getANSILength(data:String):int
		{
			data = trim(data);
			var byte:ByteArray=new ByteArray();
			byte.writeMultiByte(data, "gb2312");
			return byte.length;
		}
		
		//是否为Email地址;
		public static function isEmail(char:String):Boolean{
			if(char == null){
				return false;
			}
			char = trim(char);
			var pattern:RegExp = /(\w|[_.\-])+@((\w|-)+\.)+\w{2,4}+/; 
			var result:Object = pattern.exec(char);
			if(result == null) {
				return false;
			}
			return true;
		}
		
		//是否是数值字符串;
		public static function isNumber(char:String):Boolean{
			if(char == null){
				return false;
			}
			return !isNaN(Number(char));
		}
		
		//是否为Double型数据;
		public static function isDouble(char:String):Boolean{
			char = trim(char);
			var pattern:RegExp = /^[-\+]?\d+(\.\d+)?$/; 
			var result:Object = pattern.exec(char);
			if(result == null) {
				return false;
			}
			return true;
		}
		//Integer;
		public static function isInteger(char:String):Boolean{
			if(char == null){
				return false;
			}
			char = trim(char);
			var pattern:RegExp = /^[-\+]?\d+$/; 
			var result:Object = pattern.exec(char);
			if(result == null) {
				return false;
			}
			return true;
		}
		//English;
		public static function isEnglish(char:String):Boolean{
			if(char == null){
				return false;
			}
			char = trim(char);
			var pattern:RegExp = /^[A-Za-z]+$/; 
			var result:Object = pattern.exec(char);
			if(result == null) {
				return false;
			}
			return true;
		}
		//中文;
		public static function isChinese(char:String):Boolean{
			if(char == null){
				return false;
			}
			char = trim(char);
			var pattern:RegExp = /^[\u0391-\uFFE5]+$/; 
			var result:Object = pattern.exec(char);
			if(result == null) {
				return false;
			}
			return true;
		}
		//双字节
		public static function isDoubleChar(char:String):Boolean{
			if(char == null){
				return false;
			}
			char = trim(char);
			var pattern:RegExp = /^[^\x00-\xff]+$/; 
			var result:Object = pattern.exec(char);
			if(result == null) {
				return false;
			}
			return true;
		}
		
		//含有中文字符
		public static function hasChineseChar(char:String):Boolean{
			if(char == null){
				return false;
			}
			char = trim(char);
			var pattern:RegExp = /[^\x00-\xff]/; 
			var result:Object = pattern.exec(char);
			if(result == null) {
				return false;
			}
			return true;
		}
		//注册字符;
		public static function hasAccountChar(char:String,len:uint=15):Boolean{
			if(char == null){
				return false;
			}
			if(len < 10){
				len = 15;
			}
			char = trim(char);
			var pattern:RegExp = new RegExp("^[a-zA-Z0-9][a-zA-Z0-9_-]{0,"+len+"}$", ""); 
			var result:Object = pattern.exec(char);
			if(result == null) {
				return false;
			}
			return true;
		}
		//URL地址;
		public static function isURL(char:String):Boolean{
			if(char == null){
				return false;
			}
			char = trim(char).toLowerCase();
			var pattern:RegExp = /^http:\/\/[A-Za-z0-9]+\.[A-Za-z0-9]+[\/=\?%\-&_~`@[\]\':+!]*([^<>\"\"])*$/; 
			var result:Object = pattern.exec(char);
			if(result == null) {
				return false;
			}
			return true;
		}
		
		// 是否为空白;		
		public static function isWhitespace(char:String):Boolean{
			switch (char){
				case " ":
				case "\t":
				case "\r":
				case "\n":
				case "\f":
					return true;	
				default:
					return false;
			}
		}
		
		
		//去左右空格;
		public static function trim(char:String):String{
			if(char == null){
				return null;
			}
			return char.replace( /^\s*|\s*$/g,"");
		}
		
		//去左空格; 
		public static function ltrim(char:String):String{
			if(char == null){
				return null;
			}
			var pattern:RegExp = /^\s*/g; 
			return char.replace(pattern,"");
		}
		
		//去右空格;
		public static function rtrim(char:String):String{
			if(char == null){
				return null;
			}
			var pattern:RegExp = /\s*$/g; 
			return char.replace(pattern,"");
		}
		
	}
}